package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Address;
import com.burgeon.backend.SHOP.mappers.AddressMapper;
import com.burgeon.backend.SHOP.service.AddressService;
import com.burgeon.backend.SHOP.service.ex.AddAddressExceptionApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void setDefault(String id,String uid,String token) {
        try {
            addressMapper.SetDefaultN(uid,token);
            addressMapper.SetDefault(id,uid,token);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void DelAddress(String id, String uid, String token) {

        if ("Y".equals(getAddressById(id).getIsDefault())) {
            addressMapper.DelAddress(id);
            List<Address> addresses = getAddressByUser(uid, token);

            if (null != addresses&&addresses.size()>0){
                String aid = addresses.get(0).getId();
                addressMapper.SetDefault(aid,uid,token);
            }
        }else {
            addressMapper.DelAddress(id);
        }

    }

    @Override
    public Address getDefaultAddress(String uid, String token) {
        return addressMapper.DefaultAddress(uid, token);
    }

    @Override
    public List<Address> getAddressByUser(String uid, String token) {
        try {
            return addressMapper.GetAddressByVip(uid, token);
        } catch (Exception e) {
            System.err.println("用户获取地址失败");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addAddress(Address address) throws AddAddressExceptionApp {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateString = sdf.format(now);
        if (null!=address.getIsDefault()&&"Y".equals(address.getIsDefault())){
            addressMapper.SetDefaultN(address.getUid(), address.getToken());
        }
        //没有默认地址则设置此为默认
        if (null == getDefaultAddress(address.getUid(), address.getToken())) {
            address.setIsDefault("Y");
        }
        address.setModTime(dateString);
        address.setCreateTime(dateString);
        try {
            addressMapper.AddAddress(address);
        } catch (Exception e) {
            throw new AddAddressExceptionApp("添加地址错误");
        }
    }

    @Override
    public Address getAddressById(String id) {
        return addressMapper.GetAddressById(id);
    }

    @Override
    public void modAddress(Address address) throws AddAddressExceptionApp {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateString = sdf.format(now);
        address.setModTime(dateString);
        try {
            addressMapper.ModAddress(address);
        } catch (Exception e) {
            throw new AddAddressExceptionApp("修改地址错误");
        }
    }
}
